package com.ruoyi.project.blog.controller;

import java.util.List;
import java.util.Set;

import com.ruoyi.common.utils.bean.ResponseResult;
import com.ruoyi.framework.aspectj.lang.annotation.Anonymous;
import com.ruoyi.project.blog.domain.vo.ForgetPasswordDTO;
import com.ruoyi.project.blog.service.IBUserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.security.LoginBody;
import com.ruoyi.framework.security.service.SysLoginService;
import com.ruoyi.framework.security.service.SysPermissionService;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.blog.domain.BMenu;
import com.ruoyi.project.blog.domain.BUser;
import com.ruoyi.project.blog.service.IBMenuService;

import javax.validation.Valid;

/**
 * 登录验证
 *
 * @author ruoyi
 */
@RestController
public class BLoginController {
    @Autowired
    private SysLoginService loginService;

    @Autowired
    private IBMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private IBUserService ibUserService;

    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody) {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword());
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    @Anonymous
    @PostMapping("/registerUser")
    public AjaxResult registerUser(@RequestBody BUser bUser) {
        AjaxResult ajax = AjaxResult.success();
        ibUserService.register(bUser);
        return ajax;
    }

    @Anonymous
    @RequestMapping(value = "/forgetPassword",method = RequestMethod.PUT)
    @ApiOperation(value = "忘记密码", httpMethod = "PUT", response = ResponseResult.class, notes = "忘记密码")
    public ResponseResult forgetPassword(@Valid @RequestBody ForgetPasswordDTO forgetPasswordDTO){
        return ibUserService.forgetPassword(forgetPasswordDTO);
    }
    @RequestMapping(value = "/selectUserInfoByToken", method = RequestMethod.GET)
    @ApiOperation(value = "根据token获取用户信息", httpMethod = "GET", response = ResponseResult.class, notes = "根据token获取用户信息")
    public ResponseResult selectUserInfoByToken(String token) {
        return ibUserService.selectUserInfoByToken(token);
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @GetMapping("/getInfo")
    public AjaxResult getInfo() {
        BUser user = SecurityUtils.getLoginUser().getUser();
        user.setAvatar(user.getAvatar());
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
        return ajax;
    }

    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @GetMapping("getRouters")
    public AjaxResult getRouters() {
        Long userId = SecurityUtils.getUserId();
        List<BMenu> menus = menuService.selectMenuTreeByUserId(userId);
        return AjaxResult.success(menuService.buildMenus(menus));
    }
}
